//
//  MKRequestCenter.swift
//  MOKOAnnounce
//
//  Created by _SS on 2017/7/10.
//  Copyright © 2017年 cc.moko. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

let api:NSString = UserDefaults.standard.object(forKey: apiurlStr as String) as! NSString

func headerappend(path:NSString) -> HTTPHeaders {
    let mutableURLRequest:NSMutableURLRequest = NSMutableURLRequest.init(url: NSURL.init(string:path as String)! as URL)
    let relativePath:NSString = mutableURLRequest.url!.relativePath as NSString
    let userid = UserDefaults.standard.object(forKey: useridStr as String)
    let dict:NSDictionary = ["uri":relativePath,"udid":OpenUDID.value(),"user":userid ?? ""]
    let encryptedData:NSData = try! RNEncryptor.encryptData(dict.mj_JSONData(), with: kRNCryptorAES256Settings, password: MOKO_APP_SECRET) as NSData
    let safe_hmac:NSString = encryptedData.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0)) as NSString
    let headerValue:NSString = (MOKO_APP_KEY as NSString).appending(":").appending(safe_hmac as String) as NSString
    return ["X-MOKO-AUTH":headerValue as String,"Content-Type":"application/json"]
}

extension Response:Decodable {
    static func parse(anyData: Any) ->Response? {
        return Response(anyData: anyData)
    }
}

struct Response {
    var data:AnyObject
    init?(anyData: Any) {
        if !JSONSerialization.isValidJSONObject(anyData) {
            print("===============","不能转化")
            return nil
        }
        //json 字符串转换为data
        guard let data = try? JSONSerialization.data(withJSONObject: anyData, options: .prettyPrinted) else { return nil }
        //.allowFragment 允许json字符串最外层不是array 或 dictionary  但必须是有效的json格式
        //.mutableContainers 返回可变容器   array 或 dictionary
        if let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String : Any] {
            self.data = json as AnyObject
            print("json:",self.data)
        } else { return nil }
    }
}

// 版本管理
struct VersionRequest: Request {
    var version: String
    var path: String {
        return MOKO_VERSION_PATH + "/sysresources/versiondreamwork/" + version + "/"+MOKO_CHANNEL_TYPE
    }
    var method: HTTPMethod = .get
    var parameter: [String : AnyObject] {
        return[:]
    }
    typealias Analysis = Response
}
// get请求
struct GetRequest: HeaderRequest {
    var apiURL:NSString
    var path: String {
        return api.appending(apiURL as String)
    }
    var method: HTTPMethod = .get
    var parameter: [String : AnyObject] {
        return[:]
    }
    var header:HTTPHeaders {
        let header:HTTPHeaders = headerappend(path: path as NSString)
        return header
    }
    typealias Analysis = Response
}
// 登陆
struct LoginRequest: HeaderRequest {
    var apiURL:NSString
    var mobile: String
    var passwd: String
    
    var path: String {
        return (api as String) + (apiURL as String)
    }
    var method: HTTPMethod = .post
    var parameter: [String : AnyObject] {
        return[mobileStr as String:mobile as AnyObject,passwdStr as String:passwd as AnyObject]
    }
    var header:HTTPHeaders {
        let header:HTTPHeaders = headerappend(path: path as NSString)
        return header
    }
    typealias Analysis = Response
}


